import { createAction } from 'redux-actions'
import { delJson, body, putJson, postJson, check, parseJSON, checkStatus, data, getJson } from './util'
import fetch from 'isomorphic-fetch'
import util from '../util'

// 获取交谈周期
export 
const getRemind = createAction('get remind', (arg, cb) =>
  fetch('/conversations/schedules/remind', {
    ...postJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

export 
const addSchedule = createAction('add schedule', (arg, cb) =>
  fetch('/conversations/schedules', {
    ...postJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)


export 
const updateSchedule = createAction('update schedule', (id, arg, cb) =>
  fetch(`/conversations/schedules/${id}`, {
    ...putJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)


export 
const getSchedule = createAction('get schedule', (id, cb) =>
  fetch(`/conversations/schedules/${id}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

// 我发起的交谈对象列表
export 
const fetchSchedule = createAction('fetch schedule', (arg) =>
  fetch(`/conversations/schedules?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then((res) => {
      return {[`page_${arg.pageNumber}`]: res.data};
    })
)

export 
const fetchSourceUsersCharts = createAction('fetch source users', (arg) =>
  fetch(`/conversations/schedules/charts/sourceUsers?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then((res) => {
      return {[`page_${arg.pageNumber}`]: res.data};
    })
)

// 获取受邀请的交谈列表
export 
const fetchRecordByUser = createAction('fetch record', (arg) =>
  fetch(`/conversations/records/as-target?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then((res) => {
      return {[`page_${arg.pageNumber}`]: res.data};
    })
)

// 查看交谈记录列表
export 
const fetchRecord = createAction('fetch record', (id,arg) =>
  fetch(`/conversations/schedules/${id}/records?${util.body(arg)}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then((res) => {
      return {[`page_${arg.pageNumber}`]: res.data};
    })
)

// 查看交谈记录详情
export 
const getRecord = createAction('get getRecord', (id, cb) =>
  fetch(`/conversations/records/${id}`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

// 新增交谈记录
export 
const addRecord = createAction('add record', (arg, cb) =>
  fetch('/conversations/records', {
    ...postJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

// 修改交谈记录
export 
const updateRecord = createAction('update record', (id, arg, cb) =>
  fetch(`/conversations/records/${id}`, {
    ...putJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

export 
const chartSchedules = createAction('void', (cb) =>
  fetch(`/conversations/schedules/charts/past30Days`, getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

export 
const getchartSchedulesLine = createAction('void', (arg, cb) =>
  fetch('/conversations/schedules/charts', {
    ...postJson,
    ...body(arg)
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

export 
const clearSchedule = createAction('clear schedule');

